class FastFourierTransform48 extends WallMode {
  /*
 * FFT48 by Ike Feitler, 
   * based on: 
   * Forward FFT
   * by Damien Di Fede.
   *  
   * This sketch demonstrates uses an FFT to analyze an AudioBuffer 
   * and draw the resulting spectrum, currently as a line of colors.
   * The lines are then scrolled by, creating a sort of spectral waterfall effect.
   */

  import ddf.minim.analysis.*;
  import ddf.minim.*;
  int bright = 256;
  int zagrate = 24;
  FFT fft;
  AudioInput jingle;
  PImage proints = createImage(48, 48, HSB);

  FastFourierTransform48(PApplet parent) {
    Minim minim;

    minim = new Minim(parent);  
    jingle = minim.getLineIn(Minim.STEREO, 1024);
    fft = new FFT(jingle.bufferSize(), jingle.sampleRate());
  }

  int getDisplayTime() {
    return 45;
  }

  int getNewModeIdentifier() { 
    return 4;
  }

  float zag(float x) {
    // zagrate can be global so that we can munge it with keypresses!
    //  float zagrate = 8;
    if (x == 0) {
      return 0;
    }
    if (x%2 == 0) {   
      if (x <= bright-zagrate) { 
        x = x+zagrate;
      }
      else { 
        x--;
      }
    }
    else {
      if (x >=1+zagrate) { 
        x=x-zagrate;
      }
      else {
        x++ ;
      }
    }
    return x;
  }

  void cycle2 (PImage foo) {
    foo.loadPixels();
    for (int p=0; p < 2304 ; p++) {
      foo.pixels[p] = color(zag(red(foo.pixels[p])), zag(green(foo.pixels[p])), zag(blue(foo.pixels[p])));
    }
    foo.updatePixels();
  }

  void scrolldown(PImage dots) {
    dots.loadPixels();
    for (int x = 2304; x > 48; x--) {
      dots.pixels[x-1]=dots.pixels[x-49];
    } 
    dots.updatePixels();
  }

  PImage ccw(PImage dots) {
    PImage dout = new PImage(48, 48, RGB);
    dots.loadPixels(); 
    dout.loadPixels();
    for (int x = 0; x < 48; x++) {
      for (int y = 0; y < 48; y++) {
        dout.pixels[y+(47-x)*48] = dots.pixels[48*y+x];
      }
    }
    dout.updatePixels();
    return dout;
  }

  PImage cycleHSB(PImage dots) {
    colorMode(HSB, 255);
    dots.loadPixels();

    for (int x = 0; x<2304; x++) {
      if ( hue(dots.pixels[x]) < 255 ) {
        dots.pixels[x] = color(hue(dots.pixels[x]+7), saturation(dots.pixels[x]), brightness(dots.pixels[x]));
      }
      else 
        dots.pixels[x] = color(0, saturation(dots.pixels[x]), brightness(dots.pixels[x]));
    }
    dots.updatePixels();
    //   colorMode(RGB, 255);
    return dots;
  }

  void draw()
  {
    background(0);
    stroke(255);
    // perform a forward FFT on the samples in jingle's buffer
    // note that if jingle were a MONO file, this would be the same as using jingle.right or jingle.left
    fft.forward(jingle.mix);
    proints.loadPixels();
    //I'm only drawing the bottom 48 bands because that's what'll fit and it's roughly interesting enough
    for (int i = 0; i < 48; i++)
    {
      proints.pixels[i] = color(fft.getBand(i)*5, 255, fft.getBand(i)*5);
    }

    proints.updatePixels();
    image(ccw(proints), 0, 0);
    scrolldown(proints);
    cycleHSB(proints);
  }



  void keyPressed() {
  }
  void setup() {
  } 
  void modeSelected() {
  }
}

